<template>
  <el-dialog ref="form" title="新增员工" :visible="showDialog" @close="handleClose" @click.native="handleDialog">
    <!-- 表单 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="form.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" style="width:50%" placeholder="请输入工号" />

      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input :value="form.departmentName" style="width:50%" placeholder="请选择部门" @click.native.stop="handleClick" />
        <div v-if="isTreeShow" class="tree-box">
          <el-scrollbar>
            <el-tree

              v-loading="isTreeLoading"
              :data="treeList"
              :props="{ label: 'name' }"
              @node-click="handelNodeClick"
            />
          </el-scrollbar>
        </div>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker v-model="form.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="submitAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetDeparts } from '@/api/departments'
import { transListToTree } from '@/utils'
import obj from '@/constant/employees'
import { reqAddEmployee } from '@/api/employees'
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      hireType: obj.hireType,
      form: {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      treeList: [],
      isTreeShow: false,
      isTreeLoading: false

    }
  },
  computed: {},
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    },
    async  handleClick() {
      this.isTreeLoading = true
      this.isTreeShow = !this.isTreeShow
      const { data: { depts }} = await reqGetDeparts()
      const res = transListToTree(depts, '')
      console.log(res)
      this.treeList = res
      this.isTreeLoading = false
    },
    handleDialog() {
      this.isTreeShow = false
    },
    handelNodeClick(data) {
      // console.log(data)
      if (data.children && data.children.length > 0) return
      this.form.departmentName = data.name
      this.isTreeShow = false
    },
    submitAdd() {
      this.$refs.form.validate(async(flag) => {
        if (!flag) return
        const res = await reqAddEmployee(this.form)
        this.$message.success(res.message)
        this.handleClose()
        this.$emit('addEmployee')
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.tree-box {
  position: absolute;
  width: 50%;
  // min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  // overflow: hidden;
  background-color: #fff;
  // max-height: 200px;
  height: 200px;
  // overflow: auto;

  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
    .el-scrollbar {
      height: 100%;
    }
    .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
}
</style>
